package com.spdzen.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spdzen.mapper.ConfigMapper;
import com.spdzen.model.Config;
import com.spdzen.service.ConfigService;
import com.spdzen.support.SimpleServiceImpl;
import com.spdzen.support.exception.ResultCodeException;

/**
 * 通用设置  服务实现类
 */
@Service("configService")
public class ConfigServiceImpl extends SimpleServiceImpl<ConfigMapper, Config> implements ConfigService {	
	@Override
	public Config selectByCode(String code) {
		if(StringUtils.isBlank(code)){
			return null;
		}
		
		Config record = new Config();
		record.setCode(code);
		return baseMapper.selectOne(record);
	}
	
	@Override
	public int deleteByCode(String code) {
		if(StringUtils.isBlank(code)){
			return 0;
		}
		
		Config config = new Config();
		config.setCode(code);
		return delete(config);
	}
	
	@Override
	public int updateByCode(Config record) {
		return baseMapper.updateByCode(record);
	}
	
	@Override
	@Transactional(rollbackFor = {Exception.class})
	public void insertIfPermission(Config record) throws Exception {
		baseMapper.insert(record);
		
		Config config = new Config();
		config.setCode(record.getContent());
		if(selectCount(config) > 1) {
			throw new ResultCodeException("编号已存在！");
		}
	}
	
	@Override
	@Transactional(rollbackFor = {Exception.class})
	public void updateIfPermission(Config record) throws Exception {
		baseMapper.updateById(record);
		
		Config config = new Config();
		config.setCode(record.getContent());
		if(selectCount(config) > 1) {
			throw new ResultCodeException("编号已存在！");
		}
	}
}